﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace ChengDu.Com.WebApiService
{
    public static class ConfigManager
    {
        private static  string fullpath = Application.ExecutablePath;
        private static  string path = Path.GetDirectoryName(fullpath);
        private static  string fileName = "servicesetting.json";


        public static string GetValue(string section, string key)
        {

            var builder = new ConfigurationBuilder()
                .SetBasePath(path)
                .AddJsonFile(fileName);
            var config = builder.Build();
            var _output = config.GetSection(section + ":" + key).Value;
            return _output;
        }

        public static bool SetValue(string section, string key,string value)
        {
            string fullpath = Application.ExecutablePath;
            string path = Path.GetDirectoryName(fullpath);
            var builder = new ConfigurationBuilder()
                .SetBasePath(path)
                .AddJsonFile("servicesetting.json");
            var config = builder.Build();
            var a = config.Providers;
            try
            {
               
                config.GetSection(section + ":" + key).Value = value;
                builder.AddConfiguration(config);
            }
            catch (Exception e)
            {
                return false;
            }
            
            return true;
        }

    }
}


